package com.gaoyuzhe.controller;

import com.gaoyuzhe.feign.PaymentHystrixFeignService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Hystrix测试的客户端类
 * @author gaoyuzhe
 */
@RestController
@RequestMapping("/consumer/payment/hystrix")
@DefaultProperties(defaultFallback = "paymentDefaultMethod")
public class OrderHystrixController {
    @Autowired
    private PaymentHystrixFeignService paymentHystrixFeignService;
    /**
     * 超时测试案例
     *
     * @param id id
     * @return 结果
     */
    @RequestMapping("/paymentTimeout/{id}")
    @HystrixCommand(fallbackMethod = "paymentOkFallbackMethod",commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "1500")
    })
    public String paymentTimeout(@PathVariable("id") Integer id) {
        return paymentHystrixFeignService.paymentTimeout(id);
    }

    @RequestMapping("/paymentOk/{id}")
    @HystrixCommand
    public String paymentOk(@PathVariable("id") Integer id) {
//        int i = 10/0;
        return paymentHystrixFeignService.paymentOk(id);
    }

    public String paymentOkFallbackMethod(@PathVariable("id") Integer id){
        return "你可爱的程序出错了，支付系统繁忙，请稍等后重试";
    }

    public String paymentDefaultMethod(){
        return "默认的方法处理";
    }
}
